package com.hz.friends.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.util.DisplayMetrics;

public class UtilTools {
	public static int getScreenWidth(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int scrwidth = dm.widthPixels;
		return scrwidth;
	}

	public static int getScreenHeight(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int scrwidth = dm.heightPixels;
		return scrwidth;
	}

	/**
	 * 计算日期星座
	 * 
	 * @param month
	 * @param day
	 * @return
	 */
	public static String getConstellation(int month, int day) {
		String s = "魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
		Integer[] arr = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };
		Integer num = month * 2 - (day < arr[month - 1] ? 2 : 0);
		return s.substring(num, num + 2);
	}
	
	/**
	 * 根据用户生日计算年龄
	 */
	public static int getAgeByBirthday(Date birthday) {
		if(birthday == null)
			return 0;
		Calendar cal = Calendar.getInstance();
		
		if (cal.before(birthday)) {
//			throw new IllegalArgumentException(
//					"The birthDay is before Now.It's unbelievable!");
			return 0;
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthday);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				// monthNow==monthBirth 
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				// monthNow>monthBirth 
				age--;
			}
		}
		return age;
	}
	// 复制文件
	public static void copyFile(File sourceFile, File targetFile) throws IOException {
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

			// 缓冲数组
			byte[] b = new byte[500];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} finally {
			// 关闭流
			try {
				if (inBuff != null)
					inBuff.close();
				if (outBuff != null)
					outBuff.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
	
	public static String getdurtionTimeFormat(Date data1,Date data2){
		long durationms = (data2.getTime() - data1.getTime())/1000;
//		if(durationms == 0){
//			return "1秒前";
//		}
		if(durationms <= 1){
			return "1秒前";
 		}else if(durationms < 60){
 			return durationms+"秒前";
 		}else if(durationms <= 60*60){
 			return durationms/60+"分钟前";
 		}else if(durationms <= 60*60*24){
 			return  durationms/(60*60)+"小时前";
 		}else if(durationms <= 60*60*24*365){
 			return  durationms/(60*60*24)+"天前";
 		}else{
 			return  durationms/(60*60*24*365)+"年前";
 		}
		
	}
	
	public static File createFile(String filepath){
		try {
			String currdir = filepath.substring(0, filepath.lastIndexOf("/"));
			File  dirfile = new File(currdir);
			File file = new File(filepath);
			if(!dirfile.exists())
				dirfile.mkdirs();
			if(!file.exists())
				file.createNewFile();
			return file;
		} catch (Exception e) {
			e.printStackTrace();
 		}
	   return null;
	}
}
